import { makeAutoObservable } from 'mobx';
import { RoleUser,RoleUserFormValues} from './types';
import { getRoleUserList, createRoleUser, updateRoleUser, getInfo, deleteRoleUser, getActiveUser, getActiveRole } from '../../../../../apis/backhub/roleUser';
import { message } from 'antd';

class RoleUserStore {
  users: RoleUser[] = [];
  modalVisible = false; // 弹窗显示状态
  editingRoleUser: RoleUser | undefined = undefined; // 当前编辑的角色，undefined表示新建
  loading = false;
  roleValues: any;
  userValues: any;
  roleOptions = [];
  userOptions = [];

  constructor() {
    makeAutoObservable(this);
  }

  // 获取角色列表
  fetchRoleUsers = async () => {
    this.loading = true;
    try {
      const response = await getRoleUserList();
      this.users = (response as any).data;
    } catch (error) {
      console.error('获取角色列表失败:', error);
    } finally {
      this.loading = false;
    }
  };
  
  // 获取用户
  getUser = async (id: string) => {
    try {
      this.loading = true;
      const response = await getInfo(id);
      this.editingRoleUser =  (response as any).data;
      return this.editingRoleUser; // 返回获取到的用户数据
    } catch (error) {
      console.error('获取用户失败:', error);
      throw error;
    } finally {
      this.loading = false;
    }
  };

  // 获取角色列表
  getActiveUser = async () => {
    this.loading = true;
    try {
      this.userOptions=[];
      const response = await getActiveUser();
      this.userValues = (response as any).data;
      this.userValues && Object.entries(this.userValues).map(([id, config]) => (
        this.userOptions.push(config)
      ));
    } catch (error) {
      console.error('获取用户列表失败:', error);
    } finally {
      this.loading = false;
    }
  };

  // 获取角色列表
  getActiveRole = async () => {
    this.loading = true;
    try {
      this.roleOptions=[];
      const response = await getActiveRole();
      this.roleValues = (response as any).data;
      this.roleValues && Object.entries(this.roleValues).map(([roleId, config]) => (
        this.roleOptions.push(config)
      ));
    } catch (error) {
      console.error('获取用户列表失败:', error);
    } finally {
      this.loading = false;
    }
  };

  // 显示创建弹窗
  showCreateModal = () => {
    this.editingRoleUser = undefined;
    this.modalVisible = true;
  };

  // 显示编辑弹窗
  showEditModal = (func: RoleUser) => {
    this.editingRoleUser = func;
    this.modalVisible = true;
  };

  // 隐藏弹窗
  hideModal = () => {
    this.modalVisible = false;
    this.editingRoleUser = undefined;
  };

  // 保存角色
  saveUser = async (values: RoleUserFormValues) => {
    try {
      if (this.editingRoleUser && this.editingRoleUser.id) {
        values.userId = (values.users as any)[0]; 
        values.roleId = (values.roles as any)[0]; 
        const response = await updateRoleUser(this.editingRoleUser.id, values);
        if((response as any)?.status){
            message.success('更新成功');
        }else{
          message.error((response as any)?.msg);
        }
      } else {
        const response = await createRoleUser(values);
        if((response as any)?.status){
            message.success('创建成功');
        }else{
            message.error((response as any)?.msg);
        }
      }
      this.hideModal();
      this.fetchRoleUsers();
    } catch (error) {
      console.error('保存角色失败:', error);
    }
  };


  // 删除角色
  deleteUser = async (id: string) => {
    try {
      await deleteRoleUser(id);
      this.fetchRoleUsers();
    } catch (error) {
      console.error('删除角色失败:', error);
    }
  };
}

export const roleUserStore = new RoleUserStore();